package net.kansun.impl;

import net.kansun.Parser;

/**
 * An implementation of {@link Parser} that parses a two digits number to its corresponding English words.
 */
public class TensPlaceParser extends AbstractIrregularParser {

   private Parser unitParser;

   @Override
   public String parse(Long source) {

      final long tensQuotient = source / 10;

      if (tensQuotient == 1) {

         return getMappings().get(source);

      } else if (tensQuotient > 1) {

         Long tensUnit = tensQuotient * 10;
         Long unit = source - tensUnit;

         StringBuilder sb = new StringBuilder(String.format("%s", getMappings().get(tensUnit)));

         if (unit != 0) {
            sb.append(String.format(" %s", this.unitParser.parse(unit)));
         }
         return sb.toString();
      }

      return "";
   }

   public void setUnitParser(Parser unitParser) {
      this.unitParser = unitParser;
   }
}
